/**
 * Created on 2007-03-15 05:48
 */
package game.media.gfx;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;

/**
 * @author David Wallin
 * 
 * An entity displaying a text at a certain location
 */
public class TextEntity extends Entity
{
	/**
	 * @author David Wallin
	 * 
	 * An enum describing different justifications text can have
	 */
	public enum Justifications
	{
		/**
		 * The text will be left justified. The text will start at the specified
		 * x and y coordinates, and print out to the right
		 */
		LEFT_JUSTIFICATION,
		/**
		 * The text will be right justified. The text will start at the specified
		 * x and y coordinates, minus the length of the text, and print out to the right,
		 * making it end at the spepcified x and y coordinates
		 */
		RIGHT_JUSTIFICATION,
		/**
		 * The text will be centered justified, making it pass over the given x and
		 * y coordinates, so that they are in the middle of the text
		 */
		CENTER_JUSTIFICATION
	}
	/**
	 * The text to put where the dynamic text should be in the static one
	 */
	public static final String		DYNAMIC_TEXT = "%!DYNAMIC!%";
	/**
	 * Signals line feed
	 */
	public static final String		LINE_FEED = "%!LINEFEED!%";
	/**
	 * This text is always shown, and first.. And can only be set in the constructor, and never changed
	 */
	//private final String		m_StaticText;
	private final ArrayList<String>		m_StaticText;
	private String				m_DynamicText;
	
	private Font				m_Font;
	private Color				m_Color;
	
	private boolean				m_Flashing;
	private boolean				m_Displayed;
	private final int			m_FlashInterval = 5; //in frames
	private final int			m_DisplayTime = 20; //in frames
	private int					m_FlashTimer;
	private Justifications		m_Justification;
	private RenderManager		m_RenderManager = RenderManager.INSTANCE;
	
	/**
	 * 
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 * @param flashing if this text entity should flash or not
	 * @param justification the justification of this text
	 */
	public TextEntity(int xpos, int ypos, int width, int height, boolean flashing, Justifications justification)
	{
		this("", "", RenderManager.SERIF_12_ITALIC, Color.white, xpos, ypos, width, height, flashing, justification);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 * @param flashing if this text entity should flash or not
	 * @param justification the justification of this text
	 */
	public TextEntity(String staticText, int xpos, int ypos, int width, int height, boolean flashing, Justifications justification)
	{
		this(staticText, "", RenderManager.SERIF_12_ITALIC, Color.white, xpos, ypos, width, height, flashing, justification);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param dynamicText the text this entity should display (this text can be changed)
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 * @param flashing if this text entity should flash or not
	 * @param justification the justification of this text
	 */
	public TextEntity(String staticText, String dynamicText, int xpos, int ypos, int width, int height, boolean flashing, Justifications justification)
	{
		this(staticText, dynamicText, RenderManager.SERIF_12_ITALIC, Color.white, xpos, ypos, width, height, flashing, justification);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param dynamicText the text this entity should display (this text can be changed)
	 * @param font the font this entity should use to display its text
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 * @param flashing if this text entity should flash or not
	 * @param justification the justification of this text
	 */
	public TextEntity(String staticText, String dynamicText, Font font, int xpos, int ypos, int width, int height, boolean flashing, Justifications justification)
	{
		this(staticText, dynamicText, font, Color.white, xpos, ypos, width, height, flashing, justification);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param dynamicText the text this entity should display (this text can be changed)
	 * @param font the font this entity should use to display its text
	 * @param color the color this entity should use to display its text
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 * @param flashing if this text entity should flash or not
	 * @param justification the justification of this text
	 */
	public TextEntity(String staticText, String dynamicText, Font font, Color color, int xpos, int ypos, int width, int height, boolean flashing, Justifications justification)
	{
		super(xpos, ypos, width, height);
		m_StaticText = new ArrayList<String>();
		m_DynamicText = dynamicText;
		m_Font = font;
		m_Color = color;
		m_Flashing = flashing;
		m_Displayed = true;
		m_FlashTimer = 0;
		m_Justification = justification;
		
		String[] lines = staticText.split(LINE_FEED);
		
		for(int i = 0; i < lines.length; i++)
		{
			m_StaticText.add(lines[i]);
		}
	}
	
	/**
	 * Sets the text to display efter the static text
	 * 
	 * @param dynamicText the text to display
	 */
	public void setDynamicText(String dynamicText)
	{
		m_DynamicText = dynamicText;
	}
	
	/**
	 * Sets the font in which to display the text
	 * 
	 * @param font the font that this TextEntity should use
	 */
	public void setFont(Font font)
	{
		m_Font = font;
	}
	
	/**
	 * Sets the color in which to display the text
	 * 
	 * @param color the color that this TextEntity should use
	 */
	public void setColor(Color color)
	{
		m_Color = color;
	}
	
	/* (non-Javadoc)
	 * @see game.Entity#draw()
	 */
	@Override
	protected void doDraw()
	{
		if(m_Flashing)
		{
			m_FlashTimer++;
			
			if((m_Displayed && (m_FlashTimer > m_DisplayTime)) || (!m_Displayed && (m_FlashTimer > m_FlashInterval)))
			{
				m_FlashTimer = 0;
				m_Displayed = ! m_Displayed;
			}
		}
		
		if(m_Displayed)
		{
			int size = m_StaticText.size();
			for(int i = 0; i < size; i++)
			{
				String text = m_StaticText.get(i);
				
				if(m_StaticText.get(i).contains(DYNAMIC_TEXT))
				{
					text = m_StaticText.get(i).replaceFirst(DYNAMIC_TEXT, m_DynamicText);
				}
				
				int x = getXpos();
				
				if(m_Justification == Justifications.RIGHT_JUSTIFICATION)
				{
					x -= (text.length() * (m_Font.getSize() / 2));
				}
				
				if(m_Justification == Justifications.CENTER_JUSTIFICATION)
				{
					x -= ((text.length() / 2) * (m_Font.getSize() / 2));
				}
				
				m_RenderManager.drawString(text, x, getYpos() + (i * m_Font.getSize()), m_Font, m_Color);
			}
		}
	}
}
